package com.student.Activitys.adapter.base;

/**
 * 项目名称：
 * 类描述：公用RefreshView Adapter (上拉加载+下拉刷新)
 * 创建人：张飞祥
 * 创建时间：2017/9/18 下午2:17
 * 修改人：张飞祥
 * 修改时间：2017/9/18 下午2:17
 * 修改备注：
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.student.Activitys.adapter.viewholder.FooterViewHolder;

import java.util.List;

public abstract class BaseRefreshAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    public  List<T> mDatas;
    public  static Context mContext;
    public  static LayoutInflater mInflater;
    private static final int TYPE_ITEM   = 0;
    private static final int TYPE_FOOTER = 1;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public BaseRefreshAdapter(Context context, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    //Item布局
    protected abstract int getLayoutItmeResId();
    //Foot布局
    protected abstract int getLayoutFootResId();

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View itemView = mInflater.inflate(getLayoutItmeResId(), parent, false);
            itemView.setOnClickListener(this);
            return getViewHolder(itemView);
        } else if (viewType == TYPE_FOOTER) {
            View itemView = mInflater.inflate(getLayoutFootResId(), parent, false);
            return new FooterViewHolder(itemView);
        }
        return null;
    }

    public abstract void onBindViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public int getItemCount() {
        //RecyclerView的count设置为数据总条数+ 1（footerView）
        return mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public void addHeaderItem(List<T> items) {
        mDatas.addAll(0, items);
        notifyDataSetChanged();
    }

    public void addFooterItem(List<T> items) {
        mDatas.addAll(items);
        notifyDataSetChanged();
    }

    //Adapter中定义接口,模拟ListView的OnItemClickListener：
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }
    //将点击事件转移给外面的调用者：
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }
    //最后暴露给外面的调用者，定义一个设置Listener的方法（）：
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 更新加载更多状态
     * @param status
     */
    public abstract void changeMoreStatus(int status);

    /**
     *
     * 动态传入ViewHolder
     *
     */
    public abstract RecyclerView.ViewHolder getViewHolder(View view);
}